package com.yuerlive.rainbow.widget.game;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.RainbowApp;
import com.yuerlive.rainbow.entity.Poker;
import com.yuerlive.rainbow.module.main.presenter.MainContract;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.FormatUtil;
import com.yuerlive.rainbow.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoyi on 2017/7/14.
 * 三国 item
 */

public class SanguoLayout extends RelativeLayout {
    private float times = 2.0f;
    private ImageView icon;
    private int iconRes = R.drawable.icon_zf;
    private TextView tvName;
    String name;
    private TextView tvTotal;
    private TextView tvMyDeal;
    private TextView tvResult;
    private PokerGroup pokerGroup;
    private LinearLayout llDealBox;

    private Animation resultAnim;

    public SanguoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SanguoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private ScaleAnimation anim = (ScaleAnimation) AnimationUtils.loadAnimation(getContext(), R.anim.niuniu_scale);

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        anim.setDuration(650);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Integer.MAX_VALUE);
        icon.startAnimation(anim);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        anim.cancel();
        anim = null;
    }

    private void init(Context context, AttributeSet attrs) {
        setClipChildren(false);
        setClipToPadding(false);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SanguoLayout);
        times = array.getFloat(R.styleable.SanguoLayout_times, 2.0f);
        iconRes = array.getResourceId(R.styleable.SanguoLayout_icon, R.drawable.icon_zf);
        name = array.getString(R.styleable.SanguoLayout_name);
        array.recycle();
        View.inflate(context, R.layout.layout_game_sanguo_item, this);

        icon = (ImageView) findViewById(R.id.ivIcon);
        icon.setImageResource(iconRes);

        tvName = (TextView) findViewById(R.id.tvTimes);
        tvName.setText(name);

        tvMyDeal = (TextView) findViewById(R.id.tvMyDeal);
        tvTotal = (TextView) findViewById(R.id.tvTotal);

        pokerGroup = (PokerGroup) findViewById(R.id.pokerGroup);

        llDealBox = (LinearLayout) findViewById(R.id.llDealBox);
        llDealBox.setOnClickListener((v) -> {
            if (onDealListener != null) {
                onDealListener.onDeal(this);
            }
        });

        tvResult = (TextView) findViewById(R.id.tvResult);

        pokerGroup.hide();

        resultAnim = AnimationUtils.loadAnimation(context, R.anim.anim_sanguo_result);
        resultAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                tvResult.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tvResult.setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    public List<int[]> getPokerLocations() {
        return pokerGroup.pokerLocations;
    }

    public void setPokers(List<Poker> pokers) {
        pokerGroup.setPokers(pokers);
    }

    public void doDeal(int deal) {
        tvMyDeal.setText(FormatUtil.formatDealTotal(deal));
        L.e("NIUNIU", "deal  " + tvMyDeal.getText().toString());
    }

    private int total = 0;

    public void setDealTotal(int total) {
        if (total <= this.total && total != 0) return;
        tvTotal.setText(FormatUtil.formatDealTotal(total));
        this.total = total;
    }

    public void setResult(int result) {
        pokerGroup.setResult(result);
    }

    public void clean() {
        pokerGroup.clean();
        doDeal(0);
        setDealTotal(0);
        showConver(false);
    }

    public interface OnDealListener {
        void onDeal(View v);
    }

    private OnDealListener onDealListener;

    public void setOnDealListener(OnDealListener onDealListener) {
        this.onDealListener = onDealListener;
    }

    public void showBack() {
        pokerGroup.showBack();
    }

    public void hide() {
        pokerGroup.hide();
    }

    public PokerGroup getPokerGroup() {
        return pokerGroup;
    }

    public void showConver(boolean show) {
        ((ViewGroup) getParent()).getChildAt(1).setVisibility(show ? VISIBLE : INVISIBLE);
    }

    public void showResult(int count) {
        tvResult.setVisibility(VISIBLE);
        if (count > 0) {
            tvResult.setText("+" + count);
            tvResult.setTextColor(getResources().getColor(R.color.chat_gift));
        } else if (count < 0) {
            tvResult.setTextColor(getResources().getColor(R.color.settle_result_lose));
            tvResult.setText("" + count);
        } else {
            tvResult.setText("");
        }
        tvResult.setAnimation(resultAnim);
        resultAnim.start();
    }
}
